package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.R;
import com.itheima.common.SnowflakeUtils;
import com.itheima.mapper.backend.CategoryDao;
import com.itheima.mapper.backend.DishDao;
import com.itheima.mapper.backend.flavorDao;
import com.itheima.pojo.backend.Category;
import com.itheima.pojo.backend.Dish;
import com.itheima.pojo.backend.Flavor;
import com.itheima.pojo.backend.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class DishService {
    @Autowired
    private DishDao dishDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SnowflakeUtils snowflakeUtils;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private flavorDao flavorDao;



    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    public R select(Integer page, Integer pageSize, String name) {
        //构建分页拦截器
        PageHelper.startPage(page, pageSize);

        List<Dish> employeeList = dishDao.select(name);

        PageInfo<Dish> PageInfo = new PageInfo<>(employeeList);

        //返回值对象，这里的P类是根据前端要的数据专门写的类，。属性只有这四条
        Paging p = new Paging();
        //总条数
        p.setTotal(PageInfo.getTotal());
        //每页显示条数
        p.setSize(PageInfo.getSize());
        //当前页码
        p.setCurrent(PageInfo.getPageNum());
        //被展示的数据集
        p.setRecords(employeeList);
        return R.success(p);
    }


    /**
     * 按id查询
     * @param id
     * @return
     */
    public Dish selectByid(String id) {
        List<Flavor> seletid = flavorDao.seletid(id);
        Dish dish = dishDao.selectByid(id);
        dish.setFlavors(seletid);
        return dish;
    }


    /**
     * 添加菜品
     *
     * @param request
     * @param dish
     * @return
     */
    public R add(HttpServletRequest request, Dish dish) {

        //获取创建人
        String token = request.getHeader("Authorization");
        String s = stringRedisTemplate.opsForValue().get(token);
        dish.setCreateUser(s);
        dish.setUpdateUser(s);

        //用雪花算法获取id
        long snowflakeId = snowflakeUtils.snowflakeId();
        //设置id
        dish.setId(String.valueOf(snowflakeId));

        //获取当前时间
        long l = System.currentTimeMillis();
        dish.setCreateTime(String.valueOf(l));
        dish.setUpdateTime(String.valueOf(l));

        Category selectid = categoryDao.selectid(String.valueOf(dish.getCategoryId()));
        dish.setCategoryName(selectid.getName());



        List<Flavor> flavors = dish.getFlavors();
        for (Flavor flavor : flavors) {
            flavor.setId(String.valueOf(snowflakeUtils.snowflakeId()));
            flavor.setDishId(String.valueOf(snowflakeId));
            flavor.setCreateUser(s);
            flavor.setUpdateUser(s);
            flavor.setCreateTime(String.valueOf(l));
            flavor.setUpdateTime(String.valueOf(l));

            dishDao.flavorAdd(flavor);
        }

        int add = dishDao.add(dish);
        if (add > 0) {
            return R.success("新增菜品成功");
        } else {
            return R.error("新增菜品失败 ");
        }
    }


    /**
     *菜品下拉列表
     * @param categoryId
     * @return
     */
    public R dishlist (String categoryId){
        List<Dish> dishlist = dishDao.dishlist(categoryId);
        if (dishlist != null) {
            return R.success(dishlist);
        } else {
            return R.error("暂无菜品");
        }
    }


    /**
     * 修改
     * @param request
     * @param dish
     * @return
     */
    public boolean updatDish(HttpServletRequest request,  Dish dish) {
        //获取日期格式器
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String updateTime = format.format(System.currentTimeMillis());

        List<Flavor> flavors = dish.getFlavors();
        for (Flavor flavor : flavors) {
            flavor.setUpdateTime(updateTime);
            dishDao.updateF(flavor);
        }
        Category category = categoryDao.selectid(String.valueOf(dish.getCategoryId()));
        dish.setUpdateTime(updateTime);
        dish.setCategoryName(category.getName());
        //从token中获取当前登录用户的username,作为被修改对象的修改者值
        String token = request.getHeader("Authorization");
        String s = stringRedisTemplate.opsForValue().get(token);
        dish.setUpdateUser(s);

        return dishDao.updatDish(dish) > 0;
    }


    /**
     * 删除
     */
    public R deletdish(String[] id) {
        for (String s : id) {
            int i = dishDao.deleteDish(s);
            int deleteflavor = flavorDao.deleteflavor(s);
        }
        return R.success("删除成功");
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    public R status(String[] id, Integer status) {

        for (String s1 : id) {
            int status1 = dishDao.status(s1, status);
        }

        return R.success("修改成功");
    }
}
